﻿#pragma once

#include "ComponentBase.h"
#include "common_data.h"

class StackPlate;
class WCSSend;
enum class PlateApplyState
{
	not_need_apply = 0,	// 无需申请
	need_apply = 1,		// 需要申请
	apply_failed = 2,	// 申请失败
	apply_finish = 3,	// 申请完成
};

struct PlateStackState {
	int num;	// 托盘个数
	QString Di;	// di信息
	PlateApplyState apply;	// 请求状态
};


class PlateStack : public ComponentBase
{
	Q_OBJECT

public:
	PlateStack(QObject* parent);
	~PlateStack();

	bool GetGrabPosition(Position& pos);

	QString GetPlateStackId() { return m_info.id; }

	PlateStackState GetPlateStackState();

	QString ManualSendPlateApply();
	// 组件初始化处理函数
	virtual int ComponentInit() override;
	/*自检接口*/
	virtual bool ComponentCheckSelfState() override;
	/*复位接口*/
	virtual bool ComponentSelfReset() override;
	// 命令执行接口
	virtual bool ExcuteCmd(int cmd, const QByteArray& input, QByteArray& output) override;
	virtual bool RegisterSubscriberFunction(int type, Callback callback) override;
	virtual void ResponseSystemState(int state) override;


	int RobotSetDIInfo(const QString& di);
	int WCSSendArrive(const QByteArray& data);
private slots:
	void OnTimeToExcute();

private:
	void CheckPlate();
	void MathPlateNum();
	void SendPlateStackApply();

private:
	struct {
		QString id;
		QString rob;
		int mindi;
		int maxdi;
		int plateH;
		Position base;
	} m_info = {};
	struct {
		int num;
		int times;
		int height;
	} m_height = {};
	WCSSend* m_wcs = nullptr;

	PlateApplyState m_apply = PlateApplyState::not_need_apply;
	struct PlateState {
		bool plate;	// 托盘是否存在
		bool send;	// 是否已经发送
	};
	std::map<StackPlate*, PlateState> m_plates;
	QList<int> m_di;
	QTimer* m_time = nullptr;
	QList<Callback> m_callback;
	int m_sysState = 0;
};
